# encoding: utf-8
from datetime import date
from django import forms
from django.forms import ModelForm

from core.extensions.fields import ReCaptchaField
from core.models import Event, User, Participation, City, Discussion, Message
from core.lib import SearchOrdering


class LoginForm(forms.Form):
    login = forms.CharField(max_length=255)
    password = forms.CharField(widget=forms.PasswordInput, max_length=255)


class OpenIdForm(forms.Form):
    url = forms.CharField()


class RegisterForm(forms.Form):
    login = forms.CharField(max_length=255)
    password = forms.CharField(widget=forms.PasswordInput, max_length=255)
    password_repeat = forms.CharField(widget=forms.PasswordInput, max_length=255)
    recaptcha = ReCaptchaField()

    def clean(self):
        if not self.errors.get('password') and not self.errors.get('password_repeat'):
            if self.cleaned_data.get('password') != self.cleaned_data.get('password_repeat'):
                raise forms.ValidationError('Passwords don\'t match')

        return super(RegisterForm, self).clean()


class EventForm(ModelForm):
    description = forms.CharField(widget=forms.Textarea, max_length=10000)
    cost = forms.IntegerField(min_value=1, max_value=1000000)
    max_participants_count = forms.IntegerField(min_value=1, max_value=1000000, required=False)

    class Meta:
        model = Event
        exclude = ('owner', 'participants', 'current_sum', 'current_sum_for_user', 'status')

    def clean_date(self):
        event_date = self.cleaned_data.get('date')
        if event_date < date.today():
            raise forms.ValidationError('Date cannot be in the past.')
        return event_date


class CancelEventForm(forms.Form):
    reason = forms.CharField(widget=forms.Textarea)

    
class ParticipationForm(ModelForm):
    investment = forms.IntegerField(min_value=1, max_value=1000000)

    class Meta:
        model = Participation
        exclude = ('event', 'user')


class ProfileForm(ModelForm):
    email = forms.EmailField(required=False)
    
    class Meta:
        model = User
        exclude = ('participation_raiting', 'organization_raiting', 'cities')


class CityForm(forms.Form):
    city = forms.ChoiceField(required=False)
    
    def __init__(self, *args, **kwargs):
        cities = kwargs.pop('cities', None)
        super(CityForm, self).__init__(*args, **kwargs)
        if cities is not None:
            filtered = City.objects.exclude(id__in=cities).values_list('id', 'name')
            self.fields['city'].choices = [] + list(filtered)    


class DiscussionForm(ModelForm):
    
    class Meta:
        model = Discussion
        exclude = ('event')
        

class MessageForm(ModelForm):
    
    class Meta:
        model = Message
        exclude = ('author', 'datetime', 'discussion', 'addressee')
        

class SearchForm(forms.Form):    
    city = forms.ChoiceField(required=False)
    ordering = forms.ChoiceField(required=False)
    datetime = forms.DateTimeField(required=False)
    cost = forms.IntegerField(required=False, min_value=1, max_value=1000000)
    
    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)
        self.fields['city'].choices = [(0, 'любой')] + list(City.objects.values_list('id', 'name')) 
        self.fields['ordering'].choices = SearchOrdering.ordering